﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BulldogCMS.Core.Storage
{
    // CREATE, UPDATE, DELETE
    public class StorageEditor : IDisposable
    {
        private List<Node> newNodes = new List<Node>();


        public StorageEditor()
        {
            if (Context.Request.CurrentStorageEditor == null)
            {
                Context.Request.CurrentStorageEditor = this;
            }
            else
            {
                throw new ApplicationException("StorageEditor is already enabled.");
            }
        }

        public void DeleteNode(Node node)
        {

        }

        public Node CreateNode(Node parentNode, string nodeName, string templateName) {
            return null;
        }

        public void CreateNewVersion(Node node, string language) {
            
        }

        public void SaveChanges()
        {
            // new nodes
            foreach (Node node in newNodes)
            {
                node.ID = Context.Application.UniqueIdentifierProvider.GetNextUniqueID();

                //foreach (var version in node.Versions)
                //{
                //    version.Value.ID = Context.Application.UniqueIdentifierProvider.GetNextUniqueID();
                //}

                //Node newNode = node.ContentTreeProvider.CreateNewNode(node);

                //foreach (var version in node.Versions)
                //{
                //    NodeVersion nodeVersion = node.ContentTreeProvider.CreateNewVersion(newNode, version.Value);
                //}
            }
        }

        internal void AddNewNode(Node node)
        {
            this.newNodes.Add(node);
        }
        
        public void Dispose()
        {
            Context.Request.CurrentStorageEditor = null;
        }
    }

    //internal class FieldChange 

}